package com.shenganyuan.smac.change.adapter;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.oklib.utils.network.loader.ILoader;
import com.oklib.utils.network.loader.LoaderManager;
import com.oklib.widget.recyclerview.BaseMultiItemQuickAdapter;
import com.oklib.widget.recyclerview.BaseQuickAdapter;
import com.oklib.widget.recyclerview.BaseViewHolder;
import com.oklib.widget.recyclerview.entity.MultiItemEntity;
import com.shenganyuan.smac.R;
import com.shenganyuan.smac.common.beans.ChangeInfoSimple;
import com.shenganyuan.smac.common.beans.ItemBeanTitleBar;
import com.shenganyuan.smac.common.beans.RiskInfo;
import com.shenganyuan.smac.common.beans.RiskResource;
import com.shenganyuan.smac.common.utils.DataConverter;

import java.util.Arrays;
import java.util.List;

/**
 * @author Damon
 */
public class AdapterChangeInfo extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {


    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public AdapterChangeInfo(List<MultiItemEntity> data) {
        super(data);
        addItemType(9, R.layout.common_title_bar);
        addItemType(99, R.layout.common_risk_resource_info);
        addItemType(199, R.layout.common_recycler_with_title);
    }


    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        switch (item.getItemType()) {
            case 9:
                ItemBeanTitleBar titleBar = (ItemBeanTitleBar) item;
                ((TextView) helper.getView(R.id.txt_common_tile_bar)).setText(titleBar.title);
                break;
            case 99:
                RiskResource resource = (RiskResource) item;
                ((TextView) helper.getView(R.id.tv_check_position)).setText(resource.getBsdx());
                ((TextView) helper.getView(R.id.tv_risk_resource)).setText(resource.getBsnr());
                ((TextView) helper.getView(R.id.tv_risk_type)).setText(resource.getSglx());
                ((TextView) helper.getView(R.id.tv_risk_level)).setText(resource.getPjJb());
                ((TextView) helper.getView(R.id.tv_operation_method)).setText(resource.getDxgzcs());
                break;
            case 199:
                TextView textView = helper.getView(R.id.txt_common_3_pic_with_title);
                RecyclerView recyclerView = helper.getView(R.id.recycler_view);
                String[] picArray = null;
                //隐患信息
                if (item instanceof RiskInfo) {
                    RiskInfo riskInfo = (RiskInfo) item;
                    String picStr = riskInfo.getYhzp();
                    if (!TextUtils.isEmpty(picStr)) {
                        picArray = picStr.split(";");
                    }
                    textView.setText(riskInfo.getYhnr());
                } else if (item instanceof ChangeInfoSimple) {
                    ChangeInfoSimple changeInfoSimple = (ChangeInfoSimple) item;
                    String picStr = changeInfoSimple.getZgzp();
                    if (!TextUtils.isEmpty(picStr)) {
                        picArray = picStr.split(";");
                    }
                    textView.setText(changeInfoSimple.getZgjl());
                }
                if (picArray == null || picArray.length == 0) {
                    break;
                }
                List<String> imgs = Arrays.asList(picArray);
                BaseQuickAdapter baseQuickAdapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.common_item_image, imgs) {
                    @Override
                    protected void convert(BaseViewHolder helper, String imgUrl) {
                        LoaderManager.getLoader().loadNet(helper.getView(R.id.iv_img), imgUrl, new ILoader.Options(R.drawable.lib_img_default, R.drawable.lib_img_default));
                        helper.getView(R.id.iv_img).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                int position = helper.getAdapterPosition();
                                DataConverter.openPic(mContext, position, imgs, v);
                            }
                        });
                    }
                };
                recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
                recyclerView.setHasFixedSize(true);
                recyclerView.setAdapter(baseQuickAdapter);
                break;
        }
    }

}
